//
//  HMNewFeatureViewController.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/21.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

/// cell的数量
private let ItemCount = 4


class HMNewFeatureViewController: UICollectionViewController {
    
    /// layout
    let layout = UICollectionViewFlowLayout()
    
    /// 在这个构造函数里面给collectionView初始化一个layout
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.registerClass(HMNewFeautreCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        setupLayout()
    }
    
    /// 设置流水布局
    private func setupLayout() {
        layout.itemSize = self.view.frame.size
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 分页
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }

}

// MARK: - 数据源方法
extension HMNewFeatureViewController {
    /// 返回cell数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    /// 返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 根据Identifier 去缓存池中找 cell,如果找不到dequeue这个方法会根据注册的cell去创建一个新的cell返回给我们
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMNewFeautreCell
        
        cell.backgroundColor = UIColor.randomColor()
        
        cell.image = UIImage(named: "new_feature_\(indexPath.item + 1)")
//        cell.bkgImageView.image = UIImage(named: "new_feature_\(indexPath.item + 1)")
        
        // 只有最后一页才需要按钮
        if indexPath.item == ItemCount - 1 {
            // 最后一页
            cell.showButtons(true)
        } else {
            cell.showButtons(false)
        }
        
        return cell
    }
}
